import { applyMiddleware } from 'redux'
import fetchProxy from './fetchProxy';
import { message } from 'antd'
// 异步action中间件
const middleware = ({getState}) => next => async action => {
  const {type, callAPI, payload} = action
  // 没有异步请求直接返回action
  if (!callAPI) return next({type, payload})

  // 请求数据
  const res = await fetchProxy(callAPI)
  // 请求数据失败 提示
  if (res.status !== 200)  return message.destroy() && message.error('网络错误！')
  // 请求成功 返回data
  return next({type, payload: res.data})
}

export default applyMiddleware(middleware)